package com.lxh.lxhcrm.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lxh.lxhcrm.model.pojos.Emp;
import com.lxh.lxhcrm.model.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class EmpController {
    @Autowired
    EmpService es;


    /**
     * 登录
     * @param userName：用户名
     * @param password：密码
     * @return emp：员工对象
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Emp login(String userName, String password) {
        Emp emp = es.login(userName, password);
        return emp;
    }

    /**
     * 注册，添加员工
     * @param emp：员工对象
     */
    @RequestMapping("/add-emp")
    public void addEmp(@RequestBody Emp emp){
        System.out.println(emp+"emp");
        es.addEmp(emp);
    }

/*折叠面板 根据部门id 查询该部门下的员工信息*/
    @RequestMapping("/emp-list")
    @ResponseBody
    public Map<String,Object> findEmpById(Integer departmentId,Integer page,Integer size){
        Page<Object> obj= PageHelper.startPage(page, size);
        List<Emp> allEmp=es.findEmpById(departmentId);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",allEmp);
        return map;
    }
    /*<!--删除员工  修改is_on_job等于0-->*/
    @RequestMapping("/delete-emp")
    @ResponseBody
    public void deleteEmp(Integer empId){
        es.deleteEmp(empId);
    }

    /*<!--根据输入的名字查询员工-->*/
    @RequestMapping("/emp-like")
    @ResponseBody
    public Map<String,Object> findEmpLike(String search,Integer page,Integer size){
        Page<Object> obj= PageHelper.startPage(page, size);
        List<Emp> allEmp= es.findEmpLike(search);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",allEmp);
        return map;
    }
    /*个人中心，根据empId查询emp对象*/
    @RequestMapping("/select-empById")
    @ResponseBody
    public Emp selectEmpById(Integer empId){
        System.out.println(es.selectEmpById(empId));
        return es.selectEmpById(empId);
    }
/*个人中心修改密码*/
@RequestMapping("/edit-pwd")
    @ResponseBody
    public void editPwd(Integer empId,String psw){
        System.out.println(empId+"empId");
        System.out.println(psw+"psw");
        es.editPwd(empId,psw);
    }
    /*个人中心修改头像*/
    @RequestMapping("/change-img")
    @ResponseBody
    public void editImg(String ur,int empId){
        System.out.println(ur+"ur");
        System.out.println(empId);
        es.editImg(empId,ur);
    }
    /*新增员工时，检查该用户名是否已经被注册*/
    @RequestMapping("/check-empName")
    @ResponseBody
    public List<String> checkEmpName(String empName){
            System.out.println(empName+"empName");
        return es.checkEmpName(empName);
    }
    /*个人中心恢复默认头像*/
    @RequestMapping("/restore-img")
    @ResponseBody
    public void restoreImg(Integer empId){
        es.restoreImg(empId);
    }
    /**
     * 查询采购、库存的员工
     * @author：gmx
     * 请不要删我的代码
     */
    @RequestMapping("/cai-emp")
    public List<Emp> findCaiEmp(){
        List<Emp> emps=es.findCaiEmp();
        return emps;
    }
    @RequestMapping("/ware-emp")
    public List<Emp> findWareEmp(){
        List<Emp> emps=es.findWareEmp();
        return emps;
    }
}
